package com.cloudcc.mobile.view.im;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.im.Constant;
import com.cloudcc.mobile.im.IMHelper;
import com.cloudcc.mobile.im.ui.BaseActivity;
import com.cloudcc.mobile.im.ui.ConversationListFragment;
import com.cloudcc.mobile.view.login.LoginNewActivity;
import com.easemob.EMEventListener;
import com.easemob.EMNotifierEvent;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMMessage;
import com.easemob.util.EMLog;

/**
 * 会话列表页面
 * 
 * @author seven
 * 
 */
public class ConversationListActivity extends BaseActivity implements EMEventListener {

	private ConversationListFragment conversationListFragment;
	private BroadcastReceiver broadcastReceiver;
	private LocalBroadcastManager broadcastManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.act_conversation);
		
		
		if (getIntent().getBooleanExtra(Constant.ACCOUNT_CONFLICT, false)&&!isConflictDialogShow) {
			showConflictDialog();
		}
		
		
		registerBroadcastReceiver();

		conversationListFragment = new ConversationListFragment();
		getSupportFragmentManager().beginTransaction()
				.add(R.id.fragment_container, conversationListFragment)
				.commit();
		
		

	}

	private void registerBroadcastReceiver() {
		broadcastManager = LocalBroadcastManager.getInstance(this);
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Constant.ACTION_CONTACT_CHANAGED);
		intentFilter.addAction(Constant.ACTION_GROUP_CHANAGED);
		broadcastReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				if (conversationListFragment != null) {
					conversationListFragment.refresh();
				}
			}
		};
		broadcastManager.registerReceiver(broadcastReceiver, intentFilter);
	}
	@Override
	protected void onResume() {
		super.onResume();
		// background
		IMHelper sdkHelper = IMHelper.getInstance();
		sdkHelper.pushActivity(this);

		// register the event listener when enter the foreground
		EMChatManager.getInstance().registerEventListener(this,
				new EMNotifierEvent.Event[] { EMNotifierEvent.Event.EventNewMessage ,EMNotifierEvent.Event.EventOfflineMessage, EMNotifierEvent.Event.EventConversationListChanged});
	}

	@Override
	protected void onStop() {
		EMChatManager.getInstance().unregisterEventListener(this);
		IMHelper sdkHelper = IMHelper.getInstance();
		sdkHelper.popActivity(this);
		super.onStop();
	}

	@Override
	public void onEvent(EMNotifierEvent event) {
		switch (event.getEvent()) {
		case EventNewMessage: // 普通消息
		{
			EMMessage message = (EMMessage) event.getData();
			// 提示新消息
			IMHelper.getInstance().getNotifier().onNewMsg(message);
			refreshUIWithMessage();
			break;
		}

		case EventOfflineMessage: {
			refreshUIWithMessage();
			break;
		}

		case EventConversationListChanged: {
			refreshUIWithMessage();
		    break;
		}
		
		default:
			break;
		}
		
	}
	
	private void refreshUIWithMessage() {
		runOnUiThread(new Runnable() {
			public void run() {
					if (conversationListFragment != null) {
						conversationListFragment.refresh();
					}
			}
		});
	}
	
	private android.app.AlertDialog.Builder conflictBuilder;
	private boolean isConflictDialogShow;
	private void showConflictDialog() {
		isConflictDialogShow = true;
		IMHelper.getInstance().logout(false,null);
		String st = getResources().getString(R.string.Logoff_notification);
		if (!ConversationListActivity.this.isFinishing()) {
			// clear up global variables
			try {
				if (conflictBuilder == null)
					conflictBuilder = new android.app.AlertDialog.Builder(ConversationListActivity.this);
				conflictBuilder.setTitle(st);
				conflictBuilder.setMessage(R.string.connect_conflict);
				conflictBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						conflictBuilder = null;
						finish();
						startActivity(new Intent(ConversationListActivity.this, LoginNewActivity.class));
					}
				});
				conflictBuilder.setCancelable(false);
				conflictBuilder.create().show();
			} catch (Exception e) {
				EMLog.e(TAG, "---------color conflictBuilder error" + e.getMessage());
			}

		}

	}

}
